{% load analysis_tags %}

<style type="text/css">
.signature {
    padding: 5px;
    padding-left: 10px;
    margin-bottom: 5px;
}

.signature-process {
    background-color: #d9edf7;
    font-weight: bold;
}

.sign-key {
    font-weight: bold;
}
</style>
<section id="signatures" style="overflow-wrap: break-word;">
    <h4 style="padding-top: 5px;">Signatures</h4>
    {% if analysis.signatures %}
        {% for signature in analysis.signatures %}
            <a style="text-decoration: none;" href="#signature_{{signature.name}}" data-toggle="collapse">
            {% if signature.severity <= 1 %}
                <div class="alert alert-info signature" style="color:black">
            {% elif signature.severity == 2 %}
                <div class="alert alert-warning signature" style="color:black">
            {% elif signature.severity >= 3 %}
                <div class="alert alert-danger signature" style="color:black">
            {% endif %}
            {{signature.description}}</div></a>
            <div id="signature_{{signature.name}}" class="collapse">
            {% if signature.data %}
                {% for sign in signature.data %}
                    {% for key, value in sign.items %}
                        <div><b>{{key}}</b>: {{value}}</div>
                    {% endfor %}
                {% endfor %}
            {% else %}
                <table class="table table-bordered" style="table-layout: fixed;">
                    <tbody>
                        {% for match in signature.new_data %}
                            {% if match.process.process_name %}
                                <tr><td colspan="9" class="signature-process">Process: {{match.process.process_name}} ({{match.process.process_id}})</td></tr>
                            {% endif %}
                            {% for sign in match.signs %}
                                <tr>
                                    {% if sign.type == 'api' %}
                                        {% include "analysis/behavior/_api_call.html" with call=sign.value %}
                                        <td>
                                            <a href='#' class='call-link' data-pid='{{match.process.process_id}}' data-cid='{{sign.value.id}}'>
                                                <span class="fa fa-arrow-circle-right"></span>
                                            </a>
                                        </td>
                                    {% else %}
                                        <td colspan="2">{{sign.type}}</td>
                                        <td colspan="7">
                                            {% if sign.value|is_dict %}
                                                {% for key, value in  sign.value.items %}
                                                    <span class='sign-key'>{{key}}:</span>
                                                    <span class='sign-value'>{{value}}</span><br />
                                                {% endfor %}
                                            {% else %}
                                                {{sign.value}}
                                            {% endif %}
                                        </td>
                                    {% endif %}
                                </tr>
                            {% endfor %}
                        {% endfor %}
                    </tbody>
                </table>
            {% endif %}
            {% if signature.calls %}
                <div id="signature-calls_{{signature.name}}"></div>
                {% with data_id="signature-calls-data_"|add:signature.name %}
                    {{ signature.calls | json_script:data_id }}
                    <script type="text/javascript">
                        $("#signature_{{signature.name}}").on("show.bs.collapse", function() {
                            $.ajax(
                                "{% url "signature-calls" task_id=analysis.info.id %}",
                                {
                                    contentType: "application/json",
                                    data: document.getElementById("{{data_id}}").textContent,
                                    dataType: "html",
                                    method: "POST",
                                    success: function(responseText) {
                                        $("#signature-calls_{{signature.name}}").html(responseText);
                                    }
                                }
                            );
                        });
                    </script>
                {% endwith %}
            {% endif %}
            </div>
        {% endfor %}
    {% else %}
        <p>No signatures</p>
    {% endif %}
</section>
